/*
 *pdflib测试代码:
 *原作者的demo程序
 *Created on: 2019-7-9
 *Author: jun
 */
#include "spdf_ext.h"
#include "dataproc.h"
#include "math.h"
#include "str_ext.h"
#include "stdio.h"
#include "framework.h"

extern SPDF_Doc pdf;
extern SPDF_Page page;
static short rgb_cnt;
extern SPDF_Doc_Rec pdf_doc;

#define RGB8_INC 8
#define RGBP_INC 4

void spdf_color_rgb_per(void);
void spdf_color_rgb8(void);
void spdf_color_rgb8_board(void);
void spdf_color_rgbp_board(void);
void spdf_color_cmykp_board(void);

/*****************************************************************************
 * @brief   spdf_char_form_tab_create 输出一页符号对照表.
 * @param   none
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void spdf_color_ref(void)
{
	// spdf_color_rgb_per();
	// spdf_color_rgb8();
	// spdf_color_rgb8_board();
	// spdf_color_rgbp_board();
	spdf_color_cmykp_board();
}

void spdf_color_rgb_per(void)
{
	char r, b, g;
	int rgb_sum = 0;
	int rgb_cnt = 0, site, row, col;
	char buff[100];
	char pageout = 1;
	int x, y;

	while (rgb_sum <= 100 * 100)
	{
		int rgb_sum_seg_cnt = 0;

		for (b = 0; b <= 100; b += RGBP_INC)
		{
			for (r = 0; r <= 100; r += RGBP_INC)
			{
				for (g = 0; g <= 100; g += RGBP_INC)
				{
					// if (r + b + g != rgb_sum)
					//	continue;
					// 亮度变换
					if (r * 28 + g * 57 + b * 15 != rgb_sum)
						continue;

					rgb_sum_seg_cnt++;
					site = rgb_cnt % 200;
					row = site / 8;
					col = site % 8;
					rgb_cnt++;

					if (site == 0)
					{
						page = spdf_add_page(pdf);
						spdf_page_set_width(page, 880);
						spdf_page_set_height(page, 650);
						spdf_page_font_set(page, "SimSun", 80);
						spdf_line_width(2);
						spdf_color_line_gray(10);
						pageout = 0;
					}

					x = 5 + col * 110;
					y = 10 + row * 24;
					y = 650 - 10 - y;

					spdf_color_fill_rgb(r, g, b);
					spdf_rectangle(x, y - 23, 108, 23, 0x40);
					spdf_stroke_fill();

					sprintf(&buff[50], "%d,%d,%d", r, g, b);
					sprintf(&buff[0], "%-12s <%d>", &buff[50], rgb_sum / 10);
					spdf_color_fill_gray((spdf_rgb_to_gray(r, g, b) + 50) % 100);
					spdf_text_begin();
					spdf_text_rot_show(x + 2, y - 23 + 3, 0x40, 0, buff);
					spdf_text_end();

					if (site == 199)
					{
						spdf_page_save(page);
						pageout = 1;
					}

					if (spdf_obj_space_remain(pdf) <= 5)
						break;
				}
			}
		}

		dbg_print("rgb sum: %d , cnt :%d\r\n", rgb_sum, rgb_sum_seg_cnt);

		rgb_sum++;
	}

	if (!pageout)
	{
		spdf_page_save(page);
	}
}

void spdf_color_rgb8(void)
{
	uu16 r, b, g;
	int rgb_sum = 0;
	int rgb_cnt = 0, site, row, col;
	char buff[100];
	char pageout = 1;
	int x, y;

	while (rgb_sum <= 256 * 100)
	{
		int rgb_sum_seg_cnt = 0;

		for (b = 0; b <= 256; b += RGB8_INC)
		{
			for (r = 0; r <= 256; r += RGB8_INC)
			{
				for (g = 0; g <= 256; g += RGB8_INC)
				{
					//	continue;
					rang_limit(r, 0, 255);
					rang_limit(g, 0, 255);
					rang_limit(b, 0, 255);

					// 亮度变换
					if (r * 28 + g * 57 + b * 15 != rgb_sum)
						continue;

					rgb_sum_seg_cnt++;
					site = rgb_cnt % 200;
					row = site / 8;
					col = site % 8;
					rgb_cnt++;

					if (site == 0)
					{
						page = spdf_add_page(pdf);
						spdf_page_set_width(page, 880);
						spdf_page_set_height(page, 650);
						spdf_page_font_set(page, "SimSun", 80);
						spdf_line_width(2);
						spdf_color_line_gray(10);
						pageout = 0;
					}
					x = 5 + col * 110;
					y = 10 + row * 24;
					y = 650 - 10 - y;

					spdf_color_fill_rgb8(r, g, b);
					spdf_rectangle(x, y - 23, 108, 23, 0x40);
					spdf_stroke_fill();

					sprintf(&buff[50], "%d,%d,%d", r, g, b);
					sprintf(&buff[0], "%-12s <%d>", &buff[50], rgb_sum * 1000 / 25500);
					spdf_color_fill_gray((spdf_rgb_to_gray(r * 100 / 256, g * 100 / 256, b * 100 / 256) + 50) % 100);
					spdf_text_begin();
					spdf_text_rot_show(x + 2, y - 23 + 3, 0x40, 0, buff);
					spdf_text_end();

					if (site == 199)
					{
						spdf_page_save(page);
						pageout = 1;
					}

					if (spdf_obj_space_remain(pdf) <= 5)
						break;
				}
			}
		}

		dbg_print("rgb sum: %d , cnt :%d\r\n", rgb_sum, rgb_sum_seg_cnt);

		rgb_sum++;
	}

	if (!pageout)
	{
		spdf_page_save(page);
	}
}

void spdf_color_rgbp_board(void)
{
	uu16 r, b, g;
	int row, col;
	char buff[100];
	char pageout = 1;
	int x, y;

//////////////////////////////////////////////////////////////////////////////
	for (r = 0; r <= 100; r += 4)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 26 * 30 + 50);
		spdf_page_set_height(page, 26 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "R=%d:: X=B :: Y=G", r);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(26)
		{
			sprintf(buff, "%d", index * 4);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (g = 0; g <= 100; g += 4)
		{
			for (b = 0; b <= 100; b += 4)
			{
				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = g / 4;
				col = b / 4;

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_rgb(r, g, b);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
//////////////////////////////////////////////////////////////////////////////
	for (g = 0; g <= 100; g += 4)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 26 * 30 + 50);
		spdf_page_set_height(page, 26 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "G=%d:: X=B :: Y=R", g);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(26)
		{
			sprintf(buff, "%d", index * 4);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (r = 0; r <= 100; r += 4)
		{
			for (b = 0; b <= 100; b += 4)
			{
				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = r / 4;
				col = b / 4;

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_rgb(r, g, b);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
	//////////////////////////////////////////////////////////////////////////////
	for (b = 0; b <= 100; b += 4)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 26 * 30 + 50);
		spdf_page_set_height(page, 26 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "B=%d:: X=R :: Y=G", b);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(26)
		{
			sprintf(buff, "%d", index * 4);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (g = 0; g <= 100; g += 4)
		{
			for (r = 0; r <= 100; r += 4)
			{
				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = g / 4;
				col = r / 4;

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_rgb(r, g, b);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
}


void spdf_color_rgb8_board(void)
{
	uu16 r, b, g;
	int row, col;
	char buff[100];
	char pageout = 1;
	int x, y;

//////////////////////////////////////////////////////////////////////////////
#define RBASE
	for (r = 0; r <= 256; r += 8)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 33 * 30 + 50);
		spdf_page_set_height(page, 33 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "R=%d:: X=B :: Y=G", r != 256 ? r : 255);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(33)
		{
			sprintf(buff, "%d", index != 32 ? index * 8 : 255);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (g = 0; g <= 256; g += 8)
		{
			for (b = 0; b <= 256; b += 8)
			{
				uu16 rr, rg, rb;

				rr = r;
				rg = g;
				rb = b;

				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = g / 8;
				col = b / 8;

				//	continue;
				rang_limit(rr, 0, 255);
				rang_limit(rg, 0, 255);
				rang_limit(rb, 0, 255);

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_rgb8(rr, rg, rb);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
#define GBASE
	for (g = 0; g <= 256; g += 8)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 33 * 30 + 50);
		spdf_page_set_height(page, 33 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "G=%d:: X=B :: Y=R", g != 256 ? g : 255);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(33)
		{
			sprintf(buff, "%d", index != 32 ? index * 8 : 255);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (r = 0; r <= 256; r += 8)
		{
			for (b = 0; b <= 256; b += 8)
			{
				uu16 rr, rg, rb;

				rr = r;
				rg = g;
				rb = b;

				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = r / 8;
				col = b / 8;

				//	continue;
				rang_limit(rr, 0, 255);
				rang_limit(rg, 0, 255);
				rang_limit(rb, 0, 255);

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_rgb8(rr, rg, rb);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
	#define BBASE
	for (b = 0; b <= 256; b += 8)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 33 * 30 + 50);
		spdf_page_set_height(page, 33 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "B=%d:: X=R :: Y=G", b != 256 ? b : 255);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(33)
		{
			sprintf(buff, "%d", index != 32 ? index * 8 : 255);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (g = 0; g <= 256; g += 8)
		{
			for (r = 0; r <= 256; r += 8)
			{
				uu16 rr, rg, rb;

				rr = r;
				rg = g;
				rb = b;

				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = g / 8;
				col = r / 8;

				//	continue;
				rang_limit(rr, 0, 255);
				rang_limit(rg, 0, 255);
				rang_limit(rb, 0, 255);

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_rgb8(rr, rg, rb);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
}


void spdf_color_cmykp_board(void)
{
	uu16 r, b, g;
	int row, col;
	char buff[100];
	char pageout = 1;
	int x, y;

//////////////////////////////////////////////////////////////////////////////
	for (r = 0; r <= 100; r += 4)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 26 * 30 + 50);
		spdf_page_set_height(page, 26 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "C=%d:: X=Y :: Y=M", r);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(26)
		{
			sprintf(buff, "%d", index * 4);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (g = 0; g <= 100; g += 4)
		{
			for (b = 0; b <= 100; b += 4)
			{
				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = g / 4;
				col = b / 4;

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_cmyk(r, g, b,0);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
//////////////////////////////////////////////////////////////////////////////
	for (g = 0; g <= 100; g += 4)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 26 * 30 + 50);
		spdf_page_set_height(page, 26 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "M=%d:: X=Y :: Y=C", g);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(26)
		{
			sprintf(buff, "%d", index * 4);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (r = 0; r <= 100; r += 4)
		{
			for (b = 0; b <= 100; b += 4)
			{
				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = r / 4;
				col = b / 4;

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_cmyk(r, g, b,0);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
	//////////////////////////////////////////////////////////////////////////////
	for (b = 0; b <= 100; b += 4)
	{
		page = spdf_add_page(pdf);
		spdf_page_set_width(page, 26 * 30 + 50);
		spdf_page_set_height(page, 26 * 18 + 50);
		spdf_page_font_set(page, "SimSun", 100);
		spdf_line_width(2);
		spdf_color_line_gray(10);
		pageout = 0;

		spdf_page_font_set(page, "SimSun", 120);
		spdf_text_begin();

		sprintf(buff, "Y=%d:: X=C :: Y=M", b);
		spdf_text_rot_show(20, 5, 0x40, 0, buff);
		spdf_text_end();

		spdf_page_font_set(page, "SimSun", 100);
		spdf_text_begin();
		loop(26)
		{
			sprintf(buff, "%d", index * 4);
			spdf_text_rot_show(45 + index * 30, 25, 0x40, 0, buff);
			spdf_text_rot_show(20, 45 + index * 18, 0x40, 0, buff);
		}
		spdf_text_end();

		for (g = 0; g <= 100; g += 4)
		{
			for (r = 0; r <= 100; r += 4)
			{
				if (spdf_obj_space_remain(pdf) <= 5)
					break;

				row = g / 4;
				col = r / 4;

				x = 40 + col * 30;
				y = 40 + row * 18;

				spdf_color_fill_cmyk(r, g, b,0);
				spdf_rectangle(x, y, 29, 16, 0x40);
				spdf_stroke_fill();
			}
		}
		spdf_page_save(page);
		pageout = 1;
	}
}


